package com.kkb.interceptors;
import org.aopalliance.intercept.Interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author: 平川
 * @Description:
 * @Date Created in 2021-10-04 22:07
 * @Modified By:
 */
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Integer adminId =(Integer) session.getAttribute("adminId");
        if (adminId==null){
            response.sendRedirect("/pages/login.html");
            return false;
        }
        return true;
    }

    /**
     * 用来判断session是否为空，空则跳转到登录页面
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HttpSession session = request.getSession();
        Integer adminId =(Integer) session.getAttribute("adminId");
        if (adminId==null){
            response.sendRedirect("/pages/login.html");
        }
    }
}
